home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / sbin / update-motd < prev    next >
Encoding:
Text File  |  2009-03-09  |  4.5 KB  |  149 lines

  1. #!/bin/sh -e
  2.  
  3. ###########################################################################
  4. # /usr/sbin/update-motd
  5. #
  6. # A convenient wrapper for running the scripts located in
  7. # /etc/update-motd.d and concatenating their output to the Message of the
  8. # Day file (usually linked to /etc/motd)
  9. #
  10. # Copyright (C) 2008 Canonical Ltd.
  11. #  * Original: Aug 2008 - Dustin Kirkland <kirkland@canonical.com>
  12. #
  13. # This program is free software: you can redistribute it and/or modify
  14. # it under the terms of the GNU General Public License as published by
  15. # the Free Software Foundation, either version 3 of the License, or
  16. # (at your option) any later version.
  17. #
  18. # This program is distributed in the hope that it will be useful,
  19. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. # GNU General Public License for more details.
  22. #
  23. # You should have received a copy of the GNU General Public License
  24. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  25. #
  26. # On Debian GNU/Linux systems, the complete text of the GNU General
  27. # Public License can be found in `/usr/share/common-licenses/GPL'.
  28. ###########################################################################
  29.  
  30. NAME="update-motd"
  31. NEW=/var/run/motd.new
  32. SKEL=/etc/motd.tail
  33. REAL=/var/run/motd
  34. DIR=/etc/$NAME.d
  35. FREQ=
  36. DISABLED=/var/lib/$NAME/disabled
  37. LASTRUN=/var/run/$NAME.lastrun
  38. FORCE=0
  39.  
  40. if [ -f "$NEW" ]; then
  41.     # If /var/run/motd.new exists, another instance is running
  42.     echo "Another update-motd is running ($NEW exists)" 1>&2
  43.     exit 0
  44. fi
  45.  
  46. usage() {
  47.     echo "Usage:
  48.  
  49.   update-motd [--disable|--enable|--force] [d|hourly|daily|weekly|monthly]
  50.  
  51.     --disable - will prevent update-motd from running; useful for
  52.                 temporarily disabling automatic updates of /etc/motd
  53.                 by the /etc/cron.d/update-motd cronjob.
  54.     --enable  - will allow update-motd to run; useful for enabling
  55.                 automatic updates of /etc/motd through the
  56.                 /etc/cron.d/update-motd cronjob.
  57.     --force   - will override a disabled update-motd for a single
  58.                 update of /etc/motd.
  59.     d         - will run the scripts in /etc/$NAME.d        (Default)
  60.     hourly    - will run the scripts in /etc/$NAME.d/hourly
  61.     daily     - will run the scripts in /etc/$NAME.d/daily
  62.     weekly    - will run the scripts in /etc/$NAME.d/weekly
  63.     monthly   - will run the scripts in /etc/$NAME.d/monthly
  64. " 1>&2
  65. }
  66.  
  67. FORCE=0
  68. for arg in $@; do
  69.     case $arg in
  70.         "--disable"|"-d")
  71.             touch "$DISABLED"
  72.             echo "$NAME is now disabled." 1>&2
  73.             # Regenerate motd created at boot by
  74.             # /etc/init.d/bootmisc.sh
  75.             uname -snrvm > $REAL
  76.             [ -f $SKEL ] && cat $SKEL >> $REAL
  77.             exit 0
  78.         ;;
  79.         "--enable"|"-e")
  80.             rm -f "$DISABLED"
  81.             echo "$NAME is now enabled." 1>&2
  82.             # Do not exit here, as we want to update the MOTD
  83.             # immediately upon enabling
  84.         ;;
  85.         "--force"|"-f")
  86.             FORCE=1
  87.         ;;
  88.         "d")
  89.             FREQ=
  90.         ;;
  91.         "hourly"|"daily"|"weekly"|"monthly")
  92.             FREQ=$arg
  93.         ;;
  94.         *)
  95.             usage
  96.             exit 1
  97.         ;;
  98.     esac
  99. done
  100. DIR="$DIR/$FREQ"
  101.  
  102. if [ -f "$DISABLED" ] && [ "$FORCE" != "1" ]; then
  103.     echo "$NAME is currently disabled.
  104. You might try:
  105.   * update-motd --enable
  106.   * update-motd --force" 1>&2
  107.     exit 1
  108. fi
  109.  
  110. # Make sure we clean up the motd.new lock file if we exit for any reason
  111. trap "rm -f $NEW 2>/dev/null || true" EXIT HUP INT QUIT TERM
  112.  
  113. if [ "$FORCE" != "1" ]; then
  114.     # Be *really* nice, if we're running in the background (not forced)
  115.     # But don't fail the script if we're not able to 'nice'
  116.     renice 10 $$ >/dev/null 2>&1 || true
  117.     ionice -c3 -p $$ >/dev/null 2>&1 || true
  118. fi
  119.  
  120. # Start with the motd header
  121. # This is identical to the motd-building code used in
  122. #   /etc/init.d/bootmisc.sh
  123. uname -snrvm > $NEW
  124. [ -f $SKEL ] && cat $SKEL >> $NEW
  125.  
  126. # Create a directory to cache the script output across update-motd
  127. # runs of different frequencies
  128. mkdir -p "/var/run/$NAME"
  129. # Obtain a list of the current scripts "due" to run
  130. scripts=`run-parts --lsbsysinit --test $DIR`
  131. for script in $scripts; do
  132.     file=`basename "$script"`
  133.     # Run each script, writing to a cache file, of the same num-name
  134.     # of the script executing
  135.     $script > "/var/run/$NAME/$file" 2>/dev/null
  136. done
  137. # Now, concatenate ALL of the cached update-motd output, thus collecting
  138. # ordered output across update-motd runs of different frequencies
  139. for file in `ls /var/run/$NAME`; do
  140.     cat "/var/run/$NAME/$file" >> $NEW
  141. done
  142.  
  143. # Move the new motd into place
  144. mv -f $NEW $REAL
  145.  
  146. # Write out the current timestamp to the lastrun file
  147. echo -n "Last run completed: " > "$LASTRUN"
  148. date >> "$LASTRUN"
  149.